package com.xilihui.xlh.core.http;

import android.app.Activity;
import android.content.Intent;


import com.xilihui.xlh.BuildConfig;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.LoginActivity;
import com.xilihui.xlh.core.app.BaseApplication;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import rx.Subscriber;

/**
 * 2017/2/7.
 */
public abstract class HttpSubscriber<T extends BaseEntity> extends Subscriber<T> {

//    private static final String TAG = "HTTP";

    private Activity activity;

    protected abstract void onSuccess(T entity);

    protected abstract void onFail(String msg);

    public HttpSubscriber(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void onCompleted() {
    }

    @Override
    public void onError(Throwable e) {
        if (BuildConfig.DEBUG) e.printStackTrace();
        if (e instanceof SocketTimeoutException || e instanceof ConnectException || e instanceof UnknownHostException) {
            onFail(BaseApplication.get().getString(R.string.network_error));
            LogUtil.i("mylog","network_error");
        } else {
            onFail(e.getMessage());
            LogUtil.i("mylog","message"+e.toString());
        }
    }

    @Override
    public void onNext(T t) {
        LogUtil.i("mylog","code"+t.getStatus());
        switch (t.getStatus()) {
            case 0:
                onFail(t.getMsg());
                LogUtil.i("mylog","失败1："+t.getMsg());
                break;
            case 1:
            case 2:
            case 3:
            case 3001:
                onSuccess(t);
                break;
            case -2:
            case -3: // token不正确
                if (isLoginActivityInStack()) {// 防止打开多个登录界面
                    return;
                }
//                Intent intent = new Intent(activity, LoginActivity.class);
//                activity.startActivity(intent);
                YEventBuses.post(new YEventBuses.Event("ReLogin").setParams(t.getMsg()));
                SPUtil.put(BaseApplication.get(), SPUtil.IS_LOGIN_ACTIVITY_OPEN,true);
                SPUtil.put(BaseApplication.get(),SPUtil.IS_LOGIN,false);
                SPUtil.put(BaseApplication.get(),SPUtil.TOKEN,"");
                
                break;
            case 1002:// 帐号被禁用
                ToastUtil.toastShortNegative(t.getMsg());
            default:
                onFail(t.getMsg());
                LogUtil.i("mylog:","失败："+t.getMsg());
                break;
        }
        
    }

    private boolean isLoginActivityInStack() {
        return (boolean) SPUtil.get(BaseApplication.get(), SPUtil.IS_LOGIN_ACTIVITY_OPEN, false);
    }
}